﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using Portal.Model;

namespace Portal.Presentation.Services
{
	/// <summary>
	/// Class <see cref="SessionService"/> define session service
	/// </summary>
	public class SessionService : ISessionService
	{

		private const string USER = "USER";

		#region Fields
		private HttpSessionStateBase _session = null; 
		#endregion

		#region Properties
		/// <summary>
		/// Gets the session.
		/// </summary>
		/// <value>
		/// The session.
		/// </value>
		/// <exception cref="System.ArgumentNullException">Service is not initialized!</exception>
		protected HttpSessionStateBase Session
		{
			get
			{
				if (_session == null)
				{
					throw new Exception("Service is not initialized!");
				}
				return _session;
			}
		} 
		#endregion

		#region Methods

		/// <summary>
		/// Initializes the specified session.
		/// </summary>
		/// <param name="session">The session.</param>
		/// <exception cref="System.ArgumentNullException">session</exception>
		public void Initialize(HttpSessionStateBase session) {
			if (session == null)
			{
				throw new ArgumentNullException("session");
			}
			_session = session;
		}

		/// <summary>
		/// Gets the current user.
		/// </summary>
		/// <returns>
		/// User
		/// </returns>
		public IUser GetCurrentUser()
		{
			return (IUser)Session[USER];
		}

		/// <summary>
		/// Sets the current user.
		/// </summary>
		/// <param name="user">The user.</param>
		public void SetCurrentUser(IUser user)
		{
			Session[USER] = user;
		}

		#endregion
	}
}
